package Thread;

//龟兔赛跑
public class race implements Runnable {
	private static String winner;

	@Override
	public void run() {
		for (int i = 0; i <= 100; i++) {

			// 模拟兔子休息
			if (Thread.currentThread().getName().equals("兔子") && i % 10 == 0) {
				try {
					Thread.sleep(5);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			// 判断比赛是否结束
			boolean flag = gameOver(i);
			// 结束比赛就停止程序
			if (flag) {
				break;
			}
			System.out.println(Thread.currentThread().getName() + "跑了" + i);
		}

	}

	private boolean gameOver(int steps) {
		if (winner != null) {
			return true;
		}
		{
			if (steps >= 100) {
				winner = Thread.currentThread().getName();
				System.out.println("winner is" + winner);
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		// 创建runnable接口的实现类对象
		race bb = new race();
		// 创建线程对象，通过线程对象来开启我们的线程，代理
		new Thread(bb, "兔子").start();
		new Thread(bb, "乌龟").start();

	}
}
